package com.example.task1.ui.AllService.parking;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.ParkingScoreBean;
import com.example.task1.http.Api;
import com.example.task1.ui.BaseAdapter;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;

public class ParkingScoreAdapter extends BaseAdapter<ParkingScoreBean.RowsDTO> {

    private Context context;
    private List<ParkingScoreBean.RowsDTO> mData = new ArrayList<>();
    private TextView tvName;
    private TextView tvNum;
    private TextView tvPrice;
    private TextView tvScore;
    private TextView tvSaleQuantity;
    private Button btnExchange;

    public ParkingScoreAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void setData(List<ParkingScoreBean.RowsDTO> rows) {
        mData.clear();
        mData = rows;
        notifyDataSetChanged();
    }

    @Override
    protected int getLayout() {
        return R.layout.parkingscorelist;
    }

    @Override
    protected Context getContext() {
        return null;
    }

    @Override
    protected List<ParkingScoreBean.RowsDTO> getData() {
        return mData;
    }

    @Override
    protected void onBind(myHolder holder, ParkingScoreBean.RowsDTO date) {
        tvName.setText(date.getName());
        tvNum.setText("剩余库存：" + date.getTotal());
        tvPrice.setText("价格：" + date.getPrice());
        tvSaleQuantity.setText("销量：" + date.getSaleQuantity());
        tvScore.setText("所需积分：" + date.getScore());
        btnExchange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/api/park/score/consume/" + date.getId(), Api.getBody(new HashMap<>()));
                Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                    @Override
                    public void success(ApiResp data) {
                        MyApplication.show("兑换成功");
                        ((ParkingNumDetailActivity) context).finish();
                    }

                    @Override
                    public void error(String msg) {

                    }
                });
            }
        });
    }

    @Override
    protected void holder(View view) {
        initView(view);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private void initView(View view) {
        tvName = view.findViewById(R.id.tv_name);
        tvNum = view.findViewById(R.id.tv_num);
        tvPrice = view.findViewById(R.id.tv_price);
        tvScore = view.findViewById(R.id.tv_score);
        tvSaleQuantity = view.findViewById(R.id.tv_saleQuantity);
        btnExchange = view.findViewById(R.id.btn_exchange);
    }
}
